package com.demo.accessibility.service


import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.os.Build
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.demo.accessibility.Action
import com.demo.accessibility.app.App
import com.demo.accessibility.R

class NotificationHelper private constructor() {
    companion object {
        const val CHANNEL_ID = "nurse_channel"
        const val CHANNEL_NAME = "护士助手"
        const val CHANNEL_DESCRIPTION = "护士助手服务"
        const val NOTIFICATION_ID = 0x111
        val instance by lazy(LazyThreadSafetyMode.SYNCHRONIZED) { NotificationHelper() }
        val flags = PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
    }

    private fun getContext() = App.app

    private val mNotificationManager by lazy {
        NotificationManagerCompat.from(getContext()).apply {
            // 5.0以后需要NotificationChannel
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                createNotificationChannel(
                    NotificationChannel(
                        CHANNEL_ID,
                        CHANNEL_NAME,
                        NotificationManager.IMPORTANCE_LOW
                    ).apply {
                        setSound(null, null)
                        enableVibration(false)
                        vibrationPattern = null
                        description = CHANNEL_DESCRIPTION
                        lockscreenVisibility = Notification.VISIBILITY_PUBLIC
                    }
                )
            }
        }
    }

    fun notifyNotification() {
        mNotificationManager.notify(NOTIFICATION_ID, createNotification())
    }

    fun getNotification(): Notification {
        return createNotification()
    }

    private fun createNotification(): Notification {
        val closeAction = NotificationCompat.Action.Builder(
            0, "停止", PendingIntent.getBroadcast(
                getContext(), 0, Intent(Action.CLOSE).setPackage(getContext().packageName), flags
            )
        ).apply {
            setShowsUserInterface(false)
        }.build()
        return NotificationCompat.Builder(getContext(), CHANNEL_ID)
            .setContentTitle("服务运行中")
            .setContentText("")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setShowWhen(false)
            .setAutoCancel(false)
            .setColor(0xffEC407A.toInt())
            .setContentIntent(
                PendingIntent.getActivity(
                    getContext(),
                    0,
                    getNotificationClickIntent(),
                    flags
                )
            )
            .addAction(closeAction)
            .build()
    }

    /**
     * 点击跳转
     */
    private fun getNotificationClickIntent(): Intent? {
        val launchIntent =
            getContext().packageManager.getLaunchIntentForPackage(getContext().packageName)
        launchIntent?.also {
            it.setPackage(null)
        }
        return launchIntent
    }
}